package com.scqkxx.gamebox.activity.myself;

import android.content.Intent;
import android.text.InputType;
import android.view.View;
import com.avos.avoscloud.AVUser;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.activity.myself.loginutils.LoginRequestUtils;
import com.scqkxx.gamebox.activity.myself.loginutils.OnRequestListener;
import com.scqkxx.gamebox.databinding.ActivityModifyPasswordBinding;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.ToastUtils;
import com.scqkzqtz.base.utils.ActivityUtil;

import java.util.Map;

import io.reactivex.disposables.Disposable;

public class ModifyPasswordActivity extends BaseActivity<ActivityModifyPasswordBinding> implements View.OnClickListener {

    @Override
    public int setLayoutId() {
        return R.layout.activity_modify_password;
    }
    protected  String code  = "";
    protected String mEmailUrl = "";
    @Override
    public void init() {
        code = getIntent().getStringExtra("code");
        mEmailUrl = getIntent().getStringExtra("EmailUrl");
        binding.tvSure.setOnClickListener(this);

        binding.loginEye.setOnCheckedChangeListener((compoundButton, isChecked) -> {
            if (isChecked) {
                binding.etOriginalPw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);//显示
            } else {
                binding.etOriginalPw.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);//隐藏
            }
        });

        binding.cbNewPw.setOnCheckedChangeListener((compoundButton, isChecked) -> {
            if (isChecked) {
                binding.etNewPw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);//显示
            } else {
                binding.etNewPw.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);//隐藏
            }
        });

        binding.tvTitleView.setOnTitleClickListener(mTitleType -> {
            if (TitleView.CLICK_BACK == mTitleType) {
                finish();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sure:
                updatePw();
                break;
            default:
                break;
        }
    }

    private void updatePw() {
        LoginRequestUtils.changePw(this,mEmailUrl ,code , binding.etNewPw.getText().toString(), new OnRequestListener() {
            @Override
            public void OnVerificationSuccess() {

            }

            @Override
            public void OnVerificationError(String s) {

            }

            @Override
            public void OnSuccer(Map<String, Object> map) {
                binding.tvPwLimitError.setVisibility(View.GONE);
                AVUser.logOut();
                ToastUtils.showShortToast(getApplicationContext(), getResources().getString(R.string.pw_updated_tip));
                Intent intent = new Intent(ModifyPasswordActivity.this, LoginActivity.class);
                startActivity(intent);
                ActivityUtil.getInstance().finishActivity(MailCodeActivity.class);
                ActivityUtil.getInstance().finishActivity(ChangePsdActivity.class);
                finish();
            }

            @Override
            public void OnError(String s) {
                binding.tvPwLimitError.setVisibility(View.VISIBLE);
                binding.tvPwLimitError.setText(s);
            }

            @Override
            public void onSubscribe(Disposable d) {

            }
        });
    }
}
